﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HanGu.Entity.Enum;

namespace HanGu.CRM
{
    public class SMS
    {
        /// <summary>
        /// 接收信息手机
        /// </summary>
        public List<string> SMSTo { get; set; }
        public string Message { get; set; }
        public string ErrorsMessage { get; set; }
        /// <summary>
        /// 发送失败的号码
        /// </summary>
        public List<string> FailedTels { get; set; }
        private static readonly string softwareSerialNo = System.Configuration.ConfigurationManager.AppSettings["softwareSerialNo"];
        private static readonly string key = System.Configuration.ConfigurationManager.AppSettings["key"];
        private static readonly string serialpass = System.Configuration.ConfigurationManager.AppSettings["serialpass"];

        static emaySMS.SDKClientClient smsService = new emaySMS.SDKClientClient();
        static HanGu.Common.Loger log = new Common.Loger();
        public static int RegistEx()
        {
            try
            {
                var result = smsService.registEx(softwareSerialNo, key, serialpass);
                log.WriteGeneralLog("注册亿美短信接口", "注册结果:" + result.ToString(), Priority.Normal);
                return result;
            }
            catch
            {
                return -1;
            }
        }


        public bool Send()
        {
            bool isSuccess = false;
            if (SMSTo != null && SMSTo.Count > 0 && !string.IsNullOrEmpty(Message))
            {
                try
                {
                    foreach (var item in SMSTo)
                    {
                        isSuccess = Sended(item);
                        if (!isSuccess)
                        {
                            FailedTels.Add(item);
                        }
                    }

                }
                catch (Exception ex)
                {
                    ErrorsMessage = ex.Message;
                    //log.ExceptionHand(ex);
                }
            }
            return isSuccess;
        }

        private bool Sended(string sMSTo)
        {
            bool isSuccess = false;
            var result = smsService.sendSMS(softwareSerialNo, key, null, new string[] { sMSTo }, Message, null, "GBK", 5);
            switch (result)
            {
                case 0:
                    isSuccess = true;
                    break;
                case 17:
                    throw new Exception("发送信息失败");
                    break;
                case 18:
                    throw new Exception("发送定时信息失败");
                    break;
                case 303:
                    throw new Exception("客户端网络故障");
                    break;
                case 305:
                    throw new Exception("服务器端返回错误，错误的返回值（返回值不是数字字符串）");
                    break;
                case 307:
                    throw new Exception("目标电话号码不符合规则，电话号码必须是以0、1开头");
                    break;
                case 997:
                    throw new Exception("平台返回找不到超时的短信，该信息是否成功无法确定");
                    break;
                case 998:
                    throw new Exception("由于客户端网络问题导致信息发送超时，该信息是否成功下发无法确定");
                    break;
                case -127:
                    throw new Exception("余额不足");
                    break;
            }
            return isSuccess;
        }
    }
}